#coding:utf-8
from jsonpath_rw import parse;import re
from logs.user_log import UserLog
from mysql_connect.mysql_db import MysqlDB
mysql = MysqlDB()
log = UserLog().get_log()


class AssertRun:
    def get_res_value(self,result_json, key):
        try:
            male = parse(key).find(result_json)
            result_value = [match.value for match in male][0]
        except:
            log.error('错误：key: {} 获取响应中的value失败！'% key)
            raise Exception('错误：key: {} 获取响应中的value失败！'% key)
        else:
            return result_value

    def res_result(self,res):
        if res != None:
            try:
                res_result = res.json()
            except:
                res_result = res.text
            # log.debug('响应结果的格式为：{}'{}type(res_result))
        else:
            res_result = None
            # log.error('发送错误，响应结果为 None')
            # res__ = '响应结果为None。'
        return res_result

    def assert_main_pre(self, expect, key, res, pre_id, sql_id):
        return self.assert_main(expect, key, res, pre_id, sql_id=sql_id)

    def assert_msg(self, status, flag, res_result, expect, match_value, res_value, key, res=None):
        if flag == 'skip':
            msg = 'cookie有效；登录用例没有运行，【跳过断言】'
        else:
            res_type = type(res_result)
            str_res = str(res_result)
            if len(str_res) > 200:
                res_result = str_res[:200:]+'......(响应结果过长，这里只显示前面的一部分)'
            if res_value and len(res_value) > 200:
                res_value = res_value[:200:]+'......(响应结果过长，这里只显示前面的一部分)'
            if expect is None:
                expect = '期望结果为空，请输入期望结果！'
            pass_01 = '\n【断言通过】'
            fail_01 = '\n【断言不通过】'
            msg_01 = '响应结果是HTML页面，这里不显示'
            msg_dict = '\n期望结果 ： "{}"\n实际结果 ： "{}"\n正则匹配的值 ： "{}"\n状态码 ： "{}"\nkey ： "{}"\n实际响应结果 ： {}'.format(
                expect, res_value, match_value, status, key, res_result)
            # msg_str = '\n期望结果 ： "{}"\n实际结果 ： "{}"\n正则匹配的值 ： "{}"\n状态码 ： "{}"\nkey ： "{}"\n实际响应结果 ： {}'.format(
            # 	expect, res_value, match_value, status, key, res_result)
            if flag:
                msg = pass_01 + msg_dict
                # if res_type is dict:
                # 	msg = pass_01 + msg_dict
                # else:
                # 	msg = pass_01 + msg_str
            else:
                msg = fail_01 + msg_dict
                # if res_type == dict:
                # 	msg = fail_01 + msg_dict
                # else:
                # 	msg = fail_01 + msg_dict
        log.debug(msg + '\n\n')
        return msg

    def assert_pre(self, pre_id, msg):
        msg_01 = '\n前置用例：{}的执行结果为：'.format(pre_id)
        msg_02 = '\n-------------------------'
        msg = msg_01 + msg + msg_02
        return msg

    def assert_main(self, expect, key, res, pre_id=None, sql_id=None):
        msg = status = flag = res_result = match_value = res_value = None
        if res == 'skip':
            flag = True
        elif pre_id:
            msg_pre = self.assert_pre(pre_id, msg)
            return msg_pre
        else:
            status = res.status_code
            res_result = self.res_result(res)
            if type(res_result) == dict:
                res_value = self.get_res_value(res_result, key)
            elif type(res_result) == str:
                res_value = res_result
            match = re.search(expect, res_value)
            if match and status == 200:
                flag = True
                match_value = match.group(0)
        mysql.update_case(sql_id=sql_id, result=flag)
        msg = self.assert_msg(status, flag, res_result, expect, match_value, res_value, key, res)
        return flag, msg






